package com.gexin.controller.xapp.user;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.MoSmsUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.common.utils.email.EmailUtil;
import com.gexin.core.web.AppController;
import com.gexin.model.agent.Agent;
import com.gexin.model.weixin.WeixinUser;
import com.gexin.service.agent.AgentService;
import com.jfinal.ext.route.ControllerBind;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@ControllerBind(controllerKey = "/xapp/userBind", viewPath = "WEB-INF/system/xappLogin/")
public class UserBindController extends AppController {

	public void index(){
		setAttr("agent_id",getParaToInt());
		render("index.html");
	}




	/**
	 * 获取验证码
	 * /xapp/userBind/findcode
	 */
	public void findcode(){
		String phone = getPara("phone");
		if(StringUtil.isBlank(phone)){
			fail("电话号码不能为null!");
			return;
		}
		if(phone.length()!=11){
			fail("电话号码长度错误!");
			return;
		}
		WeixinUser weixinUser = WeixinUser.dao.findbyBindPhone(phone);
		if(weixinUser!=null&&weixinUser.getExtensionId()!=null){
			fail("您已经注册过了!请直接登录小程序或APP");
			return;
		}
		String code = StringUtil.Random(4);
		ApiResponse apiResponse = MoSmsUtils.SendCode(phone , code , getRequest());
		renderJson(apiResponse);
	}


	/**
	 * 发送文件email
	 * /xapp/userBind/sendEmail
	 *
	 */
	public void sendEmail(){
		String mail = getPara("mail");
		if(StringUtil.isBlank(mail)){
			fail("邮箱号码不能为空!");
			return;
		}
		Integer type = getParaToInt("type");
		if(type==null){
			fail("发送类型不能为空!");
			return;
		}
		WeixinUser authUser = getAuthUser();
		Agent agent = Agent.dao.findInfoByUserIdAndStatus(authUser.getId(),Agent.STATUS_WEICHULI);
		if(agent==null){
			fail("代理商不存在或没有提交代理商信息!不能发送邮件!");
			return;
		}
		//0是区域 		1是个人  	 2是二级
		String mailTital,path ;
		switch (type){
			case 0:
				path = EmailUtil.ResellerRegionalEdition;
				mailTital = "区域经销商合作合同";
				break;
			case 1:
				path = EmailUtil.dealerIindividual;
				mailTital = "个人（特约）经销商合作合同";
				break;
			case 2:
				path = EmailUtil.dealerLevelTwo;
				mailTital = "二级经销商合作合同";
				break;
			default:
				fail("类型选择错误!");
				return;
		}
		List<File> files = new ArrayList<>();
		files.add(new File(path));
		List<String> receivers = new ArrayList<>();
		receivers.add(mail);
		boolean b = EmailUtil.sendEmail(mailTital , "请及时查看合同信息，打印签字后进行邮寄。" , receivers , files);
		if(b){
			agent.setSendMail(Agent.SEND_MAIL_TRUE);
			agent.setEmail(mail);
			agent.update();
		}
		renderJson(new ApiResponse(b,b?"发送成功!":"发送失败!"));
	}



	public void extension(){
		String phone = getPara("phone");
		String code = getPara("code");
		String agentId = getPara("agent_id");
		if(StringUtil.isBlank(phone)||StringUtil.isBlank(code)||StringUtil.isBlank(agentId)){
			fail("必填项不能为null!");
			return;
		}
		boolean checkCode = MoSmsUtils.CheckCode(phone, code);
		if(!checkCode){
			fail("验证码错误!");
			return;
		}
		WeixinUser weixinUser = WeixinUser.dao.findbyBindPhone(phone);
		if(weixinUser!=null){
			fail("您已经注册过了!请直接登录小程序或APP!");
			return;
		}
		weixinUser = new WeixinUser();
		weixinUser.setBindPhone(phone);
		weixinUser.setHeadimgurl("http://img.weizhi.so/head2.jpg");
		weixinUser.setMobile(phone);
		weixinUser.setNickname("中科易购_"+phone);
		boolean save = weixinUser.save();
		renderJson(new ApiResponse(save,save?"注册成功!":"注册失败!"));
	}


}
